<?php

class Log4php {
	
	private $logFile;
	private $threshold = 1;
	private $dateFormat = 'Y-m-d H:i:s';
	private $enabled = TRUE;
	private $levels = array ('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4' );
	
	private static $instance = array ();
	
	public static function getInstance($fileName) {
		if (! array_key_exists ( $fileName, self::$instance ))
			self::$instance [$fileName] = new Log4php ( $fileName );
		self::$instance [$fileName]->logFile = $fileName;
		return self::$instance [$fileName];
	}
	
	private function __construct($fileName) {
		$this->logFile = $fileName;
		if (! is_writable ( Session::getSystemValue ( 'log_folder', false ) ))
			$this->enabled = FALSE;
		
		if (is_numeric ( Session::getSystemValue ( 'log_threshold', false ) ))
			$this->threshold = Session::getSystemValue ( 'log_threshold', false );
	}
	
	/* public static function singleton($t, $file, $dd) {
      //die($file);
      $inst = self::getInstance ( $file );
      self::$instance->logFile = $file;
      return $inst;
   }
	*/
	public function debug($msg) {
		
		$this->log ( 'debug', $msg );
	}
	
	public function error($msg) {
		$this->log ( 'error', $msg );
	}
	
	public function info($msg) {
		$this->log ( 'info', $msg );
	}
	
	public function log($level = 'error', $msg) {
		if ($this->enabled === false) {
			return false;
		}
		
		$level = strtoupper ( $level );
		
		if (! isset ( $this->levels [$level] ) || ($this->levels [$level] > $this->threshold)) {
			return false;
		}
		
		$filepath = Session::getSystemValue ( 'log_folder', false ) . $this->logFile;
		$message = '';
		
		if (! $fp = @fopen ( $filepath, 'ab' )) {
			return false;
		}
		
		$message .= $level . ' ' . (($level == 'INFO') ? ' -' : '-') . ' ' . date ( $this->dateFormat ) . ' --> ' . $msg . "\n";
		
		flock ( $fp, LOCK_EX );
		fwrite ( $fp, $message );
		flock ( $fp, LOCK_UN );
		fclose ( $fp );
		
		@chmod ( $filepath, 0666 );
		return true;
	}
	
	/**
	 * Write Log File
	 *
	 * Generally this function will be called using the global log_message() function
	 *
	 * @access	public
	 * @param	string	the error level
	 * @param	string	the error message
	 * @param	bool	whether the error is a native PHP error
	 * @return	bool
	 */
	public function write_log($level = 'error', $msg) {
		if ($this->enabled === false) {
			return false;
		}
		
		$level = strtoupper ( $level );
		
		if (! isset ( $this->levels [$level] ) or ($this->levels [$level] > $this->threshold)) {
			return false;
		}
		
		$filepath = $this->logFile . 'log-' . date ( 'Y-m-d' ) . 'log';
		$message = '';
		
		if (! $fp = @fopen ( $filepath, 'ab' )) {
			return false;
		}
		
		$message .= $level . ' ' . (($level == 'INFO') ? ' -' : '-') . ' ' . date ( $this->dateFormat ) . ' --> ' . $msg . "\n";
		
		flock ( $fp, LOCK_EX );
		fwrite ( $fp, $message );
		flock ( $fp, LOCK_UN );
		fclose ( $fp );
		
		@chmod ( $filepath, 0666 );
		return true;
	}

}
